﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Collections.Generic;

public partial class System_Pages_Settings_Extensions : SiteFox.Base.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ReloadGridView();
        }
    }
    protected void imb_Add_Click(object sender, ImageClickEventArgs e)
    {
        pan_AddOrEdit.Visible = true;
        lab_AddOrEdit_Title.Text = lib_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Add;
        ucc_AddOrEdit_Ext.ReadOnly = false;
    }
    protected void imb_Edit_Click(object sender, ImageClickEventArgs e)
    {
        pan_AddOrEdit.Visible = true;
        hf_AddOrEdit_Ext.Value = ((ImageButton)(sender)).CommandArgument;
        lab_AddOrEdit_Title.Text = lib_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Edit;
        ucc_AddOrEdit_Ext.ReadOnly = true;

        SiteFox.Business.Model.Extension _Extension = SiteFox.Business.Manager.Extension.GetByExt(hf_AddOrEdit_Ext.Value);
        ucc_AddOrEdit_Ext.Value = _Extension.Ext;
        ucc_AddOrEdit_Name.Value = _Extension.Name;
        ucc_AddOrEdit_BigIconUrl.Value = _Extension.BigIconUrl;
        ucc_AddOrEdit_SmallIconUrl.Value = _Extension.SmallIconUrl;
        ucc_AddOrEdit_Permission_Upload.Value = _Extension.AllowUpload;
        ucc_AddOrEdit_Permission_Download.Value = _Extension.AllowDownload;
        ucc_AddOrEdit_Permission_Hidden.Value = _Extension.Hidden;
        cb_AddOrEdit_New_CanCreate_Value.Checked = _Extension.AllowCreate;
    }
    protected void imb_Delete_Click(object sender, ImageClickEventArgs e)
    {
        pan_Delete.Visible = true;
        hf_Delete_Ext.Value = ((ImageButton)(sender)).CommandArgument;
    }
    //--- ACTION PANEL -> AddOrEdit
    protected void imb_AddOrEdit_Close_Click(object sender, ImageClickEventArgs e)
    {
        pan_AddOrEdit.Visible = false;
        hf_AddOrEdit_Ext.Value = "";
        lab_AddOrEdit_Title.Text = "";
        lib_AddOrEdit_Ok.Text = "";
        this.ResetAddOrEdit();
    }
    protected void lib_AddOrEdit_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Model.Extension _Extension = new SiteFox.Business.Model.Extension();
        if (hf_AddOrEdit_Ext.Value != "")
        {
            _Extension = SiteFox.Business.Manager.Extension.GetByExt(hf_AddOrEdit_Ext.Value);
        }
        _Extension.AllowCreate = cb_AddOrEdit_New_CanCreate_Value.Checked;
        //-------------------------------------------------------------
        _Extension.Ext = ucc_AddOrEdit_Ext.Value;
        _Extension.Name = ucc_AddOrEdit_Name.Value;
        _Extension.BigIconUrl = ucc_AddOrEdit_BigIconUrl.Value;
        _Extension.SmallIconUrl = ucc_AddOrEdit_SmallIconUrl.Value;
        _Extension.AllowUpload = ucc_AddOrEdit_Permission_Upload.Value;
        _Extension.AllowDownload = ucc_AddOrEdit_Permission_Download.Value;
        _Extension.Hidden = ucc_AddOrEdit_Permission_Hidden.Value;

        string _Message = "";
        if (hf_AddOrEdit_Ext.Value == "")
        {
            SiteFox.Business.Manager.Extension.Create(_Extension, out _Message);
        }
        else
        {
            SiteFox.Business.Manager.Extension.Edit(_Extension, out _Message);
        }
        this.ReloadGridView();
        pan_AddOrEdit.Visible = false;
        this.ResetAddOrEdit();
    }
    //--- ACTION PANEL -> Delete
    protected void imb_Delete_Close_Click(object sender, ImageClickEventArgs e)
    {
        pan_Delete.Visible = false;
        hf_Delete_Ext.Value = "";
    }
    protected void lib_Delete_Ok_Click(object sender, EventArgs e)
    {
        string _Message = "";
        var _Extension = SiteFox.Business.Manager.Extension.GetByExt(hf_Delete_Ext.Value);
        SiteFox.Business.Manager.Extension.Delete(_Extension.ID, out _Message);
        this.ReloadGridView();
        pan_Delete.Visible = false;
    }
    //--- COMMON METHODS
    void ReloadGridView()
    {
        gv_Extensions.DataSource = SiteFox.Business.Manager.Extension.GetAll();
        gv_Extensions.DataBind();
    }
    void ResetAddOrEdit() 
    {
        ucc_AddOrEdit_BigIconUrl.Value = "";
        ucc_AddOrEdit_Ext.Value = "";
        ucc_AddOrEdit_Name.Value = "";
        ucc_AddOrEdit_Permission_Download.Value = false;
        ucc_AddOrEdit_Permission_Upload.Value = false;
        ucc_AddOrEdit_Permission_Hidden.Value = false;
        ucc_AddOrEdit_SmallIconUrl.Value = "";
        cb_AddOrEdit_New_CanCreate_Value.Checked = false;
        hf_AddOrEdit_Ext.Value = "";
    }
}